package util;

import java.io.UnsupportedEncodingException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
public class Tool {
	public static int getCurPage(){
		ActionContext context = ActionContext.getContext(); 
		Map<String, Object> params = context.getParameters();
		if(params.containsKey("page"))
			return Integer.parseInt(((String[])params.get("page"))[0]);
		else
			return 1;
	}
	
	public static String[] splitArrayHtml(String str){
		String temp=null;
		try{
			temp=str.replaceAll(" ","");
		}catch(Exception e){
			temp="";
		}
		return temp.split(",");
	}
	
	public static String toChinese(String str){
		try {
			return new String(str.getBytes(),"ISO8859-1");
		} catch (UnsupportedEncodingException e) {}
		return null;
	}
	
	public static String toUTF8(String str){
		try {
			return new String(str.getBytes("ISO-8859-1"),"UTF-8");
		} catch (UnsupportedEncodingException e) {}
		return null;
	}
	
	//---------------------------资金大小写转换--------------------//
	public static String translateMoney(double eSender){ 
		double NewMoney; 
		int Index, len, flag; 
		String StrTemp = null; 
		char[] p; 
		String Number[]={"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"}; 
		String MonetaryUnit[] = {"万","仟","佰","拾","亿","仟","佰","拾","万","仟","佰","拾","元","角","分"} ; 
		NewMoney = Math.abs(eSender); 
		DecimalFormat deciformat; 
		deciformat=(DecimalFormat)DecimalFormat.getInstance(); 
		deciformat.applyPattern("#######"); 
		String m = java.lang.String.valueOf(deciformat.format(NewMoney * 100)); 
		int i; 
		if((i = m.indexOf('.')) != -1) 
			m = m.substring(0, i); 
		p = new char[m.length()];
		m.getChars(0, m.length(), p, 0); 
		if (NewMoney > 100000000000.00) { 
			StrTemp = ""; // FloatAsComma( NewMoney, False ); 
			return StrTemp; 
		}else if (NewMoney < 0.01) { 
			StrTemp = "零"; 
			return StrTemp; 
		} 
		if (eSender<0) { 
			StrTemp = "负"; 
		}else { 
			StrTemp = ""; 
		} 
		flag = 1; 
		len =p.length; 
		for (Index=(15-len);Index<15;Index++){ 
			if (p[Index - 15 + len]!='0'){ 
				StrTemp = StrTemp + Number[Integer.parseInt(String.valueOf(p[Index-15+len]))]; 
				StrTemp = StrTemp + MonetaryUnit[Index]; 
			}else{ 
				if (Index == 5){ 
					if ((p[Index - 14 + len]!= '0') || (p[Index-13+len] != '0')) { 
						StrTemp = StrTemp + MonetaryUnit[Index + 3]; 
						flag = 0; 
					}
				}else { 
					if ((Index == 12) ||( (Index == 8) && (flag ==1)) || (Index == 4)) { 
						StrTemp = StrTemp + MonetaryUnit[Index]; 
					} 
					if ((p[Index-15 + len] != '0') && (Index != 14)) { 
						StrTemp = StrTemp +Number[Integer.parseInt(String.valueOf(p[Index-15+len]))]; 
					} 
				} 
			} 
		} 
		if (p[m.length()-1] == '0'){ 
			StrTemp = StrTemp + "整"; 
		} 
		return StrTemp; 
		}
	
//--------------------年月日字符串的分割-----------------------------//
	public static List<String> YMD(String str){
		List<String> list=new ArrayList<String>();
		list.add(str.substring(0,4));
		list.add(str.substring(5,7));
		list.add(str.substring(8));
		return list;
	}
}
